package com.yingda.ad;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;
import com.bytedance.sdk.openadsdk.mediation.ad.MediationAdSlot;
import com.bytedance.sdk.openadsdk.mediation.manager.MediationAdEcpmInfo;
import com.bytedance.sdk.openadsdk.mediation.manager.MediationBaseManager;
import com.yingda.banner.BannerLayout;
import com.yingda.banner.adapter.WebBannerAdapter;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {
    public static final String TAG = "MainActivity1111";
    private WallpaperManager wallpaperManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 获取 WallpaperManager 实例
        wallpaperManager = WallpaperManager.getInstance(MainActivity.this);


//        属性名称 	        方法	                                           意义	           类型	    默认值
//        app:showIndicator 	setShowIndicator(boolean showIndicator)	   是否显示指示器	  boolean	true
//        app:interval	        setAutoPlayDuration(int autoPlayDuration)  轮播时间间隔	   int	     4000
//        app:isAutoPlaying	    setAutoPlaying(boolean isAutoPlaying)	   是否开启自动轮播  boolean	 true
//        app:orientation	    setOrientation(int orientation)	           轮播图方向	       enum	   horizontal
//        app:itemSpace	        setItemSpace(int itemSpace)	               图片间距	         int	 20
//        app:centerScale	   setCenterScale(float centerScale)	       当前图片缩放比列	float	 1.2
//        app:moveSpeed	       setMoveSpeed(float moveSpeed)	           滚动速度，越大越快	float	 1.0



        BannerLayout recyclerBanner =  findViewById(R.id.recycler);

        List<Integer> list = new ArrayList<>();
        list.add(R.drawable.item1);
        list.add(R.drawable.item2);
        list.add(R.drawable.item3);
        list.add(R.drawable.item4);
        list.add(R.drawable.item5);
        list.add(R.drawable.item6);
        list.add(R.drawable.item7);
        list.add(R.drawable.item8);
        list.add(R.drawable.item9);
        list.add(R.drawable.item10);

        WebBannerAdapter webBannerAdapter=new WebBannerAdapter(this,list);

        webBannerAdapter.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Integer image = list.get(position);
                launcherImage(image);
                Toast.makeText(MainActivity.this, "点击了第  " + position+"  项", Toast.LENGTH_SHORT).show();
            }
        });
        recyclerBanner.setAdapter(webBannerAdapter);




    }

    private void launcherImage(Integer image) {

        // 从资源文件中获取图片（可以替换为从存储中获取）
       // Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.item);


        // 使用 Glide 加载图片并转换为 Bitmap
        Glide.with(this)
                .asBitmap()  // 以 Bitmap 格式加载
                .load(image)  // 替换为你要加载的图片 URL
                .into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {
                        // 当 Bitmap 加载完成时，处理该 Bitmap (例如显示在 ImageView 中)


                        try {
                            // 设置图片为壁纸
                            wallpaperManager.setBitmap(bitmap);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        // 你也可以在这里将 Bitmap 传递给其他需要的地方
                    }

                    @Override
                    public void onLoadCleared(android.graphics.drawable.Drawable placeholder) {
                        // 可选：加载取消时处理
                    }
                });


    }


    private void startAd() {
        loadRewardAd(this);
    }

    //构造激励视频广告的Adlsot
    private AdSlot buildRewardAdlost(){
        return new AdSlot.Builder()
            //    .setCodeId("103166438")  //广告位ID
                .setCodeId("103169001")  //广告位ID
                .setOrientation(TTAdConstant.VERTICAL)  //激励视频方向
                .setMediationAdSlot(
                        new MediationAdSlot.Builder()
                                .setMuted(false)
                                .setRewardName("看下一个视频")
                                .setRewardAmount(1)
                                .build()
                )
                .build();
    }
    TTRewardVideoAd mttRewardVideoAd;
    //加载激励视频
    private void loadRewardAd(Activity act) {
        TTAdNative adNativeLoader = TTAdSdk.getAdManager().createAdNative(act);
        /** 这里为激励视频的简单功能，如需使用复杂功能，如gromore的服务端奖励验证，请参考demo中的AdUtils.kt类中激励部分 */
        adNativeLoader.loadRewardVideoAd(buildRewardAdlost(), new TTAdNative.RewardVideoAdListener() {
            @Override
            public void onError(int errorCode, String errorMsg) {
                //广告加载失败
                  Log.i(TAG, "onError: "+errorCode);
                Log.i("ddddd", "onError: "+errorMsg);


            }

            @Override
            public void onRewardVideoAdLoad(TTRewardVideoAd ttRewardVideoAd) {
                Log.i("ddddd", "onRewardVideoAdLoad: ");
                //广告加载成功
                  Log.i(TAG, "onRewardVideoAdLoad: "+ ttRewardVideoAd);
            }

            @Override
            public void onRewardVideoCached() {
                //广告缓存成功 此api已经废弃，请使用onRewardVideoCached(TTRewardVideoAd ttRewardVideoAd)
                  Log.i(TAG, "onRewardVideoCached: ");

            }

            @Override
            public void onRewardVideoCached(TTRewardVideoAd ttRewardVideoAd) {
                mttRewardVideoAd=ttRewardVideoAd;
                //广告缓存成功 在此回调中进行广告展示
                showRewardAd(act, ttRewardVideoAd);
            }
        });
    }

    //展示激励视频
    private void showRewardAd(Activity act, TTRewardVideoAd ttRewardVideoAd) {
        if (act == null || ttRewardVideoAd == null) {
            return;
        }

        ttRewardVideoAd.setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {
            @Override
            public void onAdShow() {
                //广告展示
                //获取展示广告相关信息，需要再show回调之后进行获取
                MediationBaseManager manager = ttRewardVideoAd.getMediationManager();
                if (manager != null && manager.getShowEcpm() != null) {
                    MediationAdEcpmInfo showEcpm = manager.getShowEcpm();
                    String ecpm = showEcpm.getEcpm(); //展示广告的价格
                    String sdkName = showEcpm.getSdkName();  //展示广告的adn名称
                    String slotId = showEcpm.getSlotId(); //展示广告的代码位ID
                }
            }

            @Override
            public void onAdVideoBarClick() {
                //广告点击
                Log.i("ddddd", "onAdVideoBarClick: ");
            }

            @Override
            public void onAdClose() {
                // 关闭

                //广告关闭
                Log.i("ddddd", "onAdClose: ");

            }

            @Override
            public void onVideoComplete() {
                //广告视频播放完成

                Log.i("ddddd", "onVideoComplete: ");


            }

            @Override
            public void onVideoError() {

                Log.i("ddddd", "onVideoError: ");

                //广告视频错误

            }

            @Override
            public void onRewardVerify(boolean rewardVerify, int rewardAmount, String rewardName, int errorCode, String errorMsg) {
                //奖励发放 已废弃 请使用 onRewardArrived 替代
            }

            @Override
            public void onRewardArrived(boolean isRewardValid, int rewardType, Bundle extraInfo) {
                //奖励发放
                if (isRewardValid) {
                    // 验证通过
                    // 从extraInfo读取奖励信息
                } else {
                    // 未验证通过
                }
            }

            @Override
            public void onSkippedVideo() {

                //广告跳过

                Log.i("ddddd", "onSkippedVideo: ");

            }
        });
        ttRewardVideoAd.showRewardVideoAd(act); //展示激励视频
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mttRewardVideoAd != null && mttRewardVideoAd.getMediationManager() != null) {
            mttRewardVideoAd.getMediationManager().destroy();
        }
    }


}